package com.zeropol2.anyquestion.component.ui.list;

import java.util.List;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.zeropol2.anyquestion.R;

/**
 * 뷰페이저
 * @author zeropol2
 *
 */
/**
 * @author zeropol2
 *
 */
public class ViewPagerLayout extends LinearLayout {

	private Context mContext;
	private LayoutInflater mInflater;
	private ViewPager mViewPager;
	private List<Fragment> mFragmentList;
	private LinearLayout mLayout;
	private OnPageChangeListener mPageChangeListener;

	private int pageCount;

	public ViewPagerLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	}

	public ViewPagerLayout(Context context) {
		super(context);
		mContext = context;
		init();
	}

	private void init() {
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLayout = (LinearLayout) mInflater.inflate(R.layout.view_pager_layout,
				this);
		mViewPager = (ViewPager) mLayout.findViewById(R.id.view_pager);
	}


	/**
	 * 뷰페이저에 프래그먼트 꽂아주기
	 * @param list
	 */
	public void setLayoutItem(List<Fragment> list) {
		mFragmentList = list;
		pageCount = list.size();
		mViewPager.setAdapter(new ViewPagerLayoutAdapter(
				((FragmentActivity) mContext).getSupportFragmentManager()));
	}

	
	/**
	 * 현재 위치 선택
	 * @param item
	 */
	public void setCurrentItem(int item) {
		mViewPager.setCurrentItem(item);
	}

	
	/**
	 * 리스너 설정
	 * @param l
	 */
	public void setOnPageChangeListener(OnPageChangeListener l) {
		mPageChangeListener = l;
		mViewPager.setOnPageChangeListener(mPageChangeListener);
	}
	
	/**
	 * 뷰페이저 아답터
	 * @author zeropol2
	 *
	 */
	private class ViewPagerLayoutAdapter extends FragmentPagerAdapter {
		public ViewPagerLayoutAdapter(FragmentManager fm) {
			super(fm);
		}

		//리스트뷰 겟뷰랑 비슷한 메소드
		@Override
		public Fragment getItem(int position) {
			return mFragmentList.get(position);
		}

		@Override
		public int getCount() {
			return pageCount;
		}
	}
}
